package com.cxyd.modules.wms.controller;

import com.cxyd.common.Utils.ExcelUtils;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.modules.wms.dto.AllotRecordDTO;
import com.cxyd.modules.wms.excel.AllotRecordExcel;
import com.cxyd.modules.wms.service.AllotRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wms/allot-record")
public class AllotRecordController {
    @Resource
    private AllotRecordService allotRecordService;

    @GetMapping("page")
    @RequiresPermissions("wms:allot-record:page")
    public Result<PageData<AllotRecordDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<AllotRecordDTO> page = allotRecordService.page(params);

        return new Result<PageData<AllotRecordDTO>>().ok(page);
    }

    @PostMapping("import")
    @LogOperation("导入调拨单")
    public Result importAllotList(@RequestParam("file") MultipartFile file) {

        return allotRecordService.allotRecordImport(file);
    }

    @PostMapping("allot")
    @LogOperation("调拨转库")
//    @RequiresPermissions("wms:allot-record:allot")
    public Result stockAllot(@RequestBody Map<String, Object> params) {
        ProcResult procResult = allotRecordService.procStockAllot(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }
    @GetMapping("export")
    public void expiryExport(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AllotRecordDTO> list = allotRecordService.getList(params);
        ExcelUtils.exportExcelToTarget(response, null, list, AllotRecordExcel.class);
    }
}
